#####
## Replication File:
##
## Who Supports Political Violence?
#####

################################################################################

####
## Load necessary packages
## 
## Analyses conducted in RStudio 1.0.153,
## using R 3.6 (Mac OS)
####

library(foreign)
library(reshape2)
library(lattice)
library(latticeExtra)
library(ggplot2)
library(ggthemes)



################################################################################

#####
## Figure 1
#####

vdata <- read.dta("violence data.dta")

pdf("figure1", width = 5, height = 5)
histogram(vdata$violence,
          aspect = 1,
          xlab = "Support for Political Violence"
)
dev.off()

################################################################################

#####
## Figure 2
#####

corrs <- read.csv("correlations.csv")

corrs$var <- reorder(corrs$var, -corrs$order)

pdf("figure2.pdf", width = 5.5, height = 6)
xyplot(as.factor(var) ~ as.numeric(corr),
       type = "p",
       aspect = 1.5, 
       xlim = c(-.3,.45),
       xlab = "Pairwise Correlation\nwith Support for Violence",
       ylab = "",
       col = "black",
       pch = 16,
       data = corrs,
       panel = function(x, y, ...) {
         panel.xyplot(x, y, ...)
         panel.abline(v=0)
         panel.abline(h=6.5, lty = 2)
         panel.abline(h=8.5, lty = 2)
         panel.abline(h=12.5, lty = 2)
         panel.abline(h=14.5, lty = 2)
         panel.abline(h=20.5, lty = 2)
         #lower horizontal cap
         panel.segments(corrs$lb,as.factor(corrs$var),   
                        corrs$ub, as.factor(corrs$var))
       }
)
dev.off()

################################################################################

#####
## Figure 3
#####

vdata <- read.dta("violence data.dta")

Egocentric	<-	vdata$egocentric
Systemic	<-	vdata$systemic
Anxiety	<-	vdata$anxiety
Powerlessness	<-	vdata$powerless
Authoritarianism <- vdata$authoritarian
Conspiracy	<-	vdata$conthink
Corrupt	<-	vdata$corrupt
Trust	<-	vdata$trustgov
Resentment	<-	vdata$raceresent
Populism	<-	vdata$populism
Insured	<-	vdata$insured
Republican	<-	vdata$rep
Pstrength	<-	vdata$pidstrength
Conservative	<-	vdata$conserv
Istrength	<-	vdata$ideostrength
Interest	<-	vdata$interest
South	<-	vdata$south
Female	<-	vdata$gender
Evangelical	<-	vdata$evangelical
Attendance	<-	vdata$attend
Military	<-	vdata$military
Income	<-	vdata$income
Whiteness	<-	vdata$whiteidentity
Nationalism <- vdata$nationalism
PID <- vdata$pid
Ideo <- vdata$ideo
Education <- vdata$edu


vdata3 <- cbind(Systemic,
                Egocentric,
                Anxiety,
                Powerlessness,
                Authoritarianism,
                Populism,
                Conspiracy,
                Corrupt,
                Trust,
                Interest,
                Whiteness,
                Resentment,
                Istrength,
                Pstrength,
                Republican,
                Conservative,
                Attendance,
                Evangelical,
                Military,
                Income,
                Insured,
                Education,
                Female,
                South
)


cormat<- cor(vdata3, use = "pairwise.complete.obs")
cormat <- round(cormat,4)
cormat[upper.tri(cormat, diag=TRUE)] <- NA
melted_cormat <- melt(cormat)


get_lower_tri<-function(cormat){
  cormat[upper.tri(cormat)] <- NA
  return(cormat)
}

lower <- get_lower_tri(cormat)
meltedlow <- melt(lower, na.rm = TRUE)


pdf("figure3", width = 5, height = 5)
ggplot(data = meltedlow, aes(x=Var1, y=Var2, fill=value)) + 
  geom_tile(color = "white") +
  theme_few() +
  theme(aspect.ratio = 1,
        axis.text.x = element_text(angle = 45, vjust = 1, 
                                   hjust = 1),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.ticks = element_blank(),
        panel.grid.major = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        legend.position = c(0.3, 0.9),
        legend.direction = "horizontal") +
  guides(fill = guide_colorbar(barwidth = 7, 
                               barheight = 1,
                               title.position = "top", title.hjust = 0.5)) +
  scale_fill_gradient2(low = "white", high = "black", mid = "gray", 
                       midpoint = 0,  space = "Lab", 
                       name="") +
  scale_x_discrete(labels = c("Egocentric Victimhood",
                              "Anxiety",
                              "Powerlessness",
                              "Authoritarianism",
                              "Populism",
                              "Conspiratorial Thinking",
                              "Perceived Corruption",
                              "Trust in Government",
                              "Interest in Politics",
                              "White Identity",
                              "Racial Resentment",
                              "Ideological Strength",
                              "Partisan Strength",
                              "Republican",
                              "Conservative",
                              "Religious Attendance",
                              "Evangelical",
                              "Military Service",
                              "Income",
                              "Health Insurance",
                              "Education",
                              "Female",
                              "Residence in South")) +
  scale_y_discrete(labels = c("Systemic Victimhood",
                              "Egocentric Victimhood",
                              "Anxiety",
                              "Powerlessness",
                              "Authoritarianism",
                              "Populism",
                              "Conspiratorial Thinking",
                              "Perceived Corruption",
                              "Trust in Government",
                              "Interest in Politics",
                              "White Identity",
                              "Racial Resentment",
                              "Ideological Strength",
                              "Partisan Strength",
                              "Republican",
                              "Conservative",
                              "Religious Attendance",
                              "Evangelical",
                              "Military Service",
                              "Income",
                              "Health Insurance",
                              "Education",
                              "Female")) 
dev.off()

################################################################################

#####
## Figure 5
#####

hist1 <- histogram(~as.factor(justified),
                   data = vdata,
                   aspect = 1,
                   xlab = "Support for Capitol Riots"
)

hist2 <- histogram(~as.factor(profile),
                   data = vdata,
                   aspect = 1,
                   xlab = "Political Violence Profile"
)

pdf("figure5.pdf", width = 8, height = 4)
print(hist1, position = c(0, 0, .5, 1), more = TRUE)
print(hist2, position = c(.5, 0, 1, 1), more = FALSE)
dev.off()

################################################################################

#####
## Figure 5
#####

preds <- read.dta("Justified Predictions.dta")

my.panel.bands <-
  function(x, y, upper, lower,
           fill, col,
           subscripts, ..., font, fontface)
  {
    upper <- upper[subscripts]
    lower <- lower[subscripts]
    panel.polygon(c(x, rev(x)), c(upper, rev(lower)),
                  col = fill, border = FALSE,
                  ...)
  }

pdf("figure6.pdf", width=6, height=4)
xyplot(estimate ~ scale | as.factor(order),
       data = preds,
       aspect = 1,
       xlab = "",
       ylab = "Predicted Riot Support",
       ylim = c(1, 3.6),
       groups = order,
       col = "black",
       type = "l",
       fill = c("light grey"),
       upper = preds$upper,
       lower = preds$lower,
       strip=strip.custom(factor.levels=c("Trump Support", 
                                          "Political Violence Profile")),
       panel = function(x, y, ...){
         panel.superpose(x, y, panel.groups = 'my.panel.bands', ...)
         panel.xyplot(x, y, ...)
       }
)
dev.off()

################################################################################

#####
## Figure 7
#####

mfx <- read.dta("Justified Marginal Effects.dta")

pdf("figure7.pdf", width = 5, height = 5)
xyplot(estimate ~ scale,
       data = mfx,
       aspect = 1,
       ylim = c(0.5, 3.5),
       type = "l",
       xlab = "Trump Support",
       ylab = "Marginal Effect of Political Violence Profile",
       col = "black",
       panel = function(x, y, ...){
         panel.xyplot(x, y, col="black", type="l")
         panel.xyplot(mfx$scale, mfx$lower, lty=2, type="l", col="black")
         panel.xyplot(mfx$scale, mfx$upper, lty=2, type="l", col="black")
       }
)
dev.off()

################################################################################

#####
## Figure A1
#####

set.seed(1234)
plot1 <- xyplot(violence ~ egocentric,
                data = vdata,
                aspect = 1,
                xlab = "Egocentric Victimhood",
                ylab = "Support for Political Violence",
                panel = function(x, y, ...){
                  panel.xyplot(jitter(x), jitter(y), col="dark grey")
                  panel.smoother(x, y)
                  panel.lmline(x, y, col="red")
                }
)

set.seed(1234)
plot2 <- xyplot(violence ~ systemic,
                data = vdata,
                aspect = 1,
                xlab = "Systemic Victimhood",
                ylab = "Support for Political Violence",
                panel = function(x, y, ...){
                  panel.xyplot(jitter(x), jitter(y), col="dark grey")
                  panel.smoother(x, y)
                  panel.lmline(x, y, col="red")
                }
)

set.seed(1234)
plot3 <- xyplot(violence ~ anxiety,
                data = vdata,
                aspect = 1,
                xlab = "Anxiety",
                ylab = "Support for Political Violence",
                panel = function(x, y, ...){
                  panel.xyplot(jitter(x), jitter(y), col="dark grey")
                  panel.smoother(x, y)
                  panel.lmline(x, y, col="red")
                }
)

set.seed(1234)
plot4 <- xyplot(violence ~ powerless,
                data = vdata,
                aspect = 1,
                xlab = "Powerlessness",
                ylab = "Support for Political Violence",
                panel = function(x, y, ...){
                  panel.xyplot(jitter(x), jitter(y), col="dark grey")
                  panel.smoother(x, y)
                  panel.lmline(x, y, col="red")
                }
)

set.seed(1234)
plot5 <- xyplot(violence ~ authoritarian,
                data = vdata,
                aspect = 1,
                xlab = "Authoritarianism",
                ylab = "Support for Political Violence",
                panel = function(x, y, ...){
                  panel.xyplot(jitter(x), jitter(y), col="dark grey")
                  panel.smoother(x, y)
                  panel.lmline(x, y, col="red")
                }
)

set.seed(1234)
plot6 <- xyplot(violence ~ populism,
                data = vdata,
                aspect = 1,
                xlab = "Populism",
                ylab = "Support for Political Violence",
                panel = function(x, y, ...){
                  panel.xyplot(jitter(x), jitter(y), col="dark grey")
                  panel.smoother(x, y)
                  panel.lmline(x, y, col="red")
                }
)

set.seed(1234)
plot7 <- xyplot(violence ~ conthink,
                data = vdata,
                aspect = 1,
                xlab = "Conspiratorial Thinking",
                ylab = "Support for Political Violence",
                panel = function(x, y, ...){
                  panel.xyplot(jitter(x), jitter(y), col="dark grey")
                  panel.smoother(x, y)
                  panel.lmline(x, y, col="red")
                }
)

set.seed(1234)
plot8 <- xyplot(violence ~ corrupt,
                data = vdata,
                aspect = 1,
                xlab = "Perceived Corruption",
                ylab = "Support for Political Violence",
                panel = function(x, y, ...){
                  panel.xyplot(jitter(x), jitter(y), col="dark grey")
                  panel.smoother(x, y)
                  panel.lmline(x, y, col="red")
                }
)

set.seed(1234)
plot9 <- xyplot(violence ~ trustgov,
                data = vdata,
                aspect = 1,
                xlab = "Trust in Government",
                ylab = "Support for Political Violence",
                panel = function(x, y, ...){
                  panel.xyplot(jitter(x), jitter(y), col="dark grey")
                  panel.smoother(x, y)
                  panel.lmline(x, y, col="red")
                }
)

set.seed(1234)
plot10 <- xyplot(violence ~ interest,
                 data = vdata,
                 aspect = 1,
                 xlab = "Interest in Politics",
                 ylab = "Support for Political Violence",
                 panel = function(x, y, ...){
                   panel.xyplot(jitter(x), jitter(y), col="dark grey")
                   panel.smoother(x, y)
                   panel.lmline(x, y, col="red")
                 }
)

set.seed(1234)
plot11 <- xyplot(violence ~ whiteidentity,
                 data = vdata,
                 aspect = 1,
                 xlab = "White Identity",
                 ylab = "Support for Political Violence",
                 panel = function(x, y, ...){
                   panel.xyplot(jitter(x), jitter(y), col="dark grey")
                   panel.smoother(x, y)
                   panel.lmline(x, y, col="red")
                 }
)

set.seed(1234)
plot12 <- xyplot(violence ~ raceresent,
                 data = vdata,
                 aspect = 1,
                 xlab = "Racial Resentment",
                 ylab = "Support for Political Violence",
                 panel = function(x, y, ...){
                   panel.xyplot(jitter(x), jitter(y), col="dark grey")
                   panel.smoother(x, y)
                   panel.lmline(x, y, col="red")
                 }
)

set.seed(1234)
plot13 <- xyplot(violence ~ ideostrength,
                 data = vdata,
                 aspect = 1,
                 xlab = "Ideological Strength",
                 ylab = "Support for Political Violence",
                 panel = function(x, y, ...){
                   panel.xyplot(jitter(x), jitter(y), col="dark grey")
                   panel.smoother(x, y)
                   panel.lmline(x, y, col="red")
                 }
)

set.seed(1234)
plot14 <- xyplot(violence ~ pidstrength,
                 data = vdata,
                 aspect = 1,
                 xlab = "Partisan Strength",
                 ylab = "Support for Political Violence",
                 panel = function(x, y, ...){
                   panel.xyplot(jitter(x), jitter(y), col="dark grey")
                   panel.smoother(x, y)
                   panel.lmline(x, y, col="red")
                 }
)

set.seed(1234)
plot15 <- xyplot(violence ~ rep,
                 data = vdata,
                 aspect = 1,
                 xlim = c(-0.5, 1.5),
                 xlab = "Republican?",
                 ylab = "Support for Political Violence",
                 scales = list(x = list(at = c(0, 1), label = c("No", "Yes"))),
                 panel = function(x, y, ...){
                   panel.xyplot(jitter(x), jitter(y), col="dark grey")
                   panel.smoother(x, y)
                   panel.lmline(x, y, col="red")
                 }
)

set.seed(1234)
plot16 <- xyplot(violence ~ conserv,
                 data = vdata,
                 aspect = 1,
                 xlim = c(-0.5, 1.5),
                 xlab = "Conservative?",
                 ylab = "Support for Political Violence",
                 scales = list(x = list(at = c(0, 1), label = c("No", "Yes"))),
                 panel = function(x, y, ...){
                   panel.xyplot(jitter(x), jitter(y), col="dark grey")
                   panel.smoother(x, y)
                   panel.lmline(x, y, col="red")
                 }
)

set.seed(1234)
plot17 <- xyplot(violence ~ attend,
                 data = vdata,
                 aspect = 1,
                 xlab = "Religiosity",
                 ylab = "Support for Political Violence",
                 panel = function(x, y, ...){
                   panel.xyplot(jitter(x), jitter(y), col="dark grey")
                   panel.smoother(x, y)
                   panel.lmline(x, y, col="red")
                 }
)

set.seed(1234)
plot18 <- xyplot(violence ~ evangelical,
                 data = vdata,
                 aspect = 1,
                 xlim = c(-0.5, 1.5),
                 xlab = "Evangelical?",
                 ylab = "Support for Political Violence",
                 scales = list(x = list(at = c(0, 1), label = c("No", "Yes"))),
                 panel = function(x, y, ...){
                   panel.xyplot(jitter(x), jitter(y), col="dark grey")
                   panel.smoother(x, y)
                   panel.lmline(x, y, col="red")
                 }
)

pdf("figureA1-1.pdf", width=8, height=8)
print(plot1, position = c(0, .5, .5, 1), more = T)
print(plot2, position = c(.5, .5, 1, 1), more = T)
print(plot3, position = c(0, 0, .5, .5), more = T)
print(plot4, position = c(.5, 0, 1, .5), more = F)
dev.off()

pdf("figureA1-2.pdf", width=8, height=8)
print(plot5, position = c(0, .5, .5, 1), more = T)
print(plot6, position = c(.5, .5, 1, 1), more = T)
print(plot7, position = c(0, 0, .5, .5), more = T)
print(plot8, position = c(.5, 0, 1, .5), more = F)
dev.off()

pdf("figureA1-3.pdf", width=8, height=8)
print(plot9, position = c(0, .5, .5, 1), more = T)
print(plot10, position = c(.5, .5, 1, 1), more = T)
print(plot11, position = c(0, 0, .5, .5), more = T)
print(plot12, position = c(.5, 0, 1, .5), more = F)
dev.off()

pdf("figureA1-4.pdf", width=8, height=8)
print(plot13, position = c(0, .5, .5, 1), more = T)
print(plot14, position = c(.5, .5, 1, 1), more = T)
print(plot15, position = c(0, 0, .5, .5), more = T)
print(plot16, position = c(.5, 0, 1, .5), more = F)
dev.off()

pdf("figureA1-5.pdf", width=8, height=8)
print(plot17, position = c(0, .5, .5, 1), more = T)
print(plot18, position = c(.5, .5, 1, 1), more = f)
dev.off()

################################################################################

#####
## Figure A2
#####

preds <- read.dta("Justified Predictions, NBREG.dta")

my.panel.bands <-
  function(x, y, upper, lower,
           fill, col,
           subscripts, ..., font, fontface)
  {
    upper <- upper[subscripts]
    lower <- lower[subscripts]
    panel.polygon(c(x, rev(x)), c(upper, rev(lower)),
                  col = fill, border = FALSE,
                  ...)
  }

pdf("figureA2.pdf", width=6, height=4)
xyplot(estimate ~ scale | as.factor(order),
       data = preds,
       aspect = 1,
       xlab = "",
       ylab = "Predicted Riot Support",
       ylim = c(1, 3.6),
       groups = order,
       col = "black",
       type = "l",
       fill = c("light grey"),
       upper = preds$upper,
       lower = preds$lower,
       strip=strip.custom(factor.levels=c("Trump Support", 
                                          "Political Violence Profile")),
       panel = function(x, y, ...){
         panel.superpose(x, y, panel.groups = 'my.panel.bands', ...)
         panel.xyplot(x, y, ...)
       }
)
dev.off()

################################################################################

#####
## Figure A3
#####

mfx <- read.dta("Justified Marginal Effects, NBREG.dta")

pdf("figureA3.pdf", width = 5, height = 5)
xyplot(estimate ~ scale,
       data = mfx,
       aspect = 1,
       ylim = c(-.5, 3.5),
       type = "l",
       xlab = "Trump Support",
       ylab = "Marginal Effect of Political Violence Profile",
       col = "black",
       panel = function(x, y, ...){
         panel.xyplot(x, y, col="black", type="l")
         panel.xyplot(mfx$scale, mfx$lower, lty=2, type="l", col="black")
         panel.xyplot(mfx$scale, mfx$upper, lty=2, type="l", col="black")
       }
)
dev.off()


